/*
 *
 */
package org.opu.gui.docviewer;

import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.opu.gui.docviewer.table.TableEditorKit;

/**
 *
 * @author yura
 */
public class DocDocumentReader {

    /** word document parsed data storege */
    private HWPFDocument wordDocument;

    /** reglament word document range to datadocument paragraphs */
    private DocumentParser parser;

    /**
     *  Create object with seted word document
     *
     * @param wordD word document parsed data storege
     */
    public DocDocumentReader(HWPFDocument wordD) {
        this.wordDocument = wordD;
        this.parser = new DocumentParser(wordD);
        
    }

    /**
     *  Create object with seted word document,
     * readed this document from current file
     *
     * @param fileName name of file, where from data will be readed
     * @throws java.io.IOException if any I/O error: file not found,
     * file wrong data format ...
     */
    public DocDocumentReader(String fileName) throws IOException {
        this(initHWFDocument(fileName));
    }

    /**
     *  Create object with out seted word document
     * <b>Note</b>: Before call method parseDocument you needed
     * to seted document
     */
    public DocDocumentReader() {
        wordDocument = null;
    }

    /**
     * Get the value of wordDocument
     *
     * @return the value of wordDocument
     */
    public HWPFDocument getWordDocument() {
        return wordDocument;
    }

    /**
     * Set the value of wordDocument
     *
     * @param wordDocument new value of wordDocument
     */
    public void setWordDocument(HWPFDocument wordDocument) {
        this.wordDocument = wordDocument;
    }

    /**
     * init parsing word document
     * @return HWPFDocument
     */
    private static HWPFDocument initHWFDocument(String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(fileName);
        POIFSFileSystem fs = new POIFSFileSystem(fis);
        HWPFDocument doc = new HWPFDocument(fs);
        return doc;
    }

    /**
     * @return Document
     * @throws java.lang.IllegalArgumentException if trable in
     *  initializing paragraphs
     */
    public void parseDocument() {
        Range ranges = wordDocument.getRange();
        int rcount = ranges.numParagraphs();

        for (int i = 0; i < rcount; i++) {
            Paragraph p = null;
            int type;

            try {
                p = ranges.getParagraph(i);
                type = p.type();
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            } catch (NullPointerException e) {
                continue;
            }

            /*switch (type + p.type()){
                case Range.TYPE_TABLE:
                    i = appendTable(doc, p);
                    break;
                case Range.TYPE_TEXT:
                case Range.TYPE_UNDEFINED:
                default:
                    doc.elementsAdd(getText(p));
                    break;
            }*/

            String s = p.text();
            if (p.isInTable()) {
                Table table = ranges.getTable(p);
                int kol = parser.appendTable(table);
                if (kol > 0) {
                    i += kol - 1;
                }
            } else {
                parser.append(p);
            }
        }
    }

    public StyledDocument viewDocument(StyleContext styles) {
        return parser.getStyleDocument();
    }

    public StyledEditorKit getStyledEditorKit() {
        return new TableEditorKit();
    }
}
